package com.axend.aerosense.room.ui.activity;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomActivitySceneLearnBinding;
import com.axend.aerosense.room.ui.fragment.RoomSceneLearnKnowFragment;
import com.axend.aerosense.room.ui.fragment.RoomSceneLearnTimeFragment;
import com.gyf.immersionbar.ImmersionBar;

public class RoomSceneLearnActivity extends MvvmBaseActivity<RoomActivitySceneLearnBinding, BaseViewModel> {

    public static final String RADAR_ID = "RADAR_ID";
    public static final String STUDY_TIME = "STUDY_TIME";
    private boolean isBack = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
//        ARouter.getInstance().inject(this);
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true).init();
        setSupportActionBar(viewDataBinding.roomToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        if (getIntent().getIntExtra(STUDY_TIME, 0) > 0) {
//            天数大于0
            bindFragment(RoomSceneLearnTimeFragment.getInstance());
        } else {
//            天数＜=0
            bindFragment(RoomSceneLearnKnowFragment.getInstance());
        }
    }

    private void bindFragment(Fragment fragment) {
        FragmentUtil.add(this, viewDataBinding.roomSceneContent.getId(), fragment, null, false);
    }

    public void setBack(boolean back) {
        isBack = back;
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.room_activity_scene_learn;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    public void setTitle(String title) {
        viewDataBinding.roomTitle.setText(title);
    }

    //返回键
    @Override
    public void onBackPressed() {
//        Toast.makeText(getApplicationContext(), "onBackPressed", Toast.LENGTH_SHORT).show();
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            finish();
            return;
        }

//        FragmentUtils.show(getSupportFragmentManager());
        if (isBack) {
            super.onBackPressed();
        } else {
            finish();
        }
    }

    //左返回键事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public int getContentId() {
        return viewDataBinding.roomSceneContent.getId();
    }

}
